#include "MyMainWindow.h"
#include "MyListView.h"
#include "MyListViewItem.h"

#include <stdlib.h>

#include <qlayout.h>
#include <qpushbutton.h>
#include <qpopupmenu.h>

MyMainWindow::MyMainWindow()
	: QMainWindow(0, "MyMainWindow", WDestructiveClose)
	, m_bShowOffline(true)
{
	setCaption(tr("qt3alk"));
	myListView = new MyListView(this);

	for (int i = 0; i < 5; i++)
		clickedAdd();

	QPushButton * buttonAdd = new QPushButton(this);
	buttonAdd->setText(tr("+Add"));
	connect(buttonAdd, SIGNAL(clicked()), SLOT(clickedAdd()));

	QPushButton * buttonView = new QPushButton(this);
	buttonView->setText(tr("View"));

    m_menuView = new QPopupMenu(buttonView);

	m_menuView->insertItem(tr("Show one page"), viewShowOnePage);

	m_menuView->insertItem(tr("Sort by name"), viewSortByName);
	m_menuView->connectItem(viewSortByName, this, SLOT(onSortByName()));

	m_menuView->insertItem(tr("Show pictures"), viewShowPictures);
	m_menuView->connectItem(viewShowPictures, this, SLOT(onShowPictures()));
	m_menuView->setItemChecked(viewShowPictures, myListView->m_bShowPicture);

	m_menuView->insertItem(tr("Show offline friends"), viewShowOffline);
	m_menuView->connectItem(viewShowOffline, this, SLOT(onShowOffline()));
	m_menuView->setItemChecked(viewShowOffline, m_bShowOffline);

	m_menuView->insertItem(tr("Show all address book contacts"), viewShowAllAddressBookContacts);
	m_menuView->setItemChecked(viewShowAllAddressBookContacts, true);
	m_menuView->setItemEnabled(viewShowAllAddressBookContacts, false);
	

    buttonView->setPopup(m_menuView);

	QHBoxLayout * hbox = new QHBoxLayout;
	hbox->setMargin(2);
	hbox->setSpacing(2);
	hbox->addWidget(buttonAdd);
	hbox->addWidget(buttonView);
	hbox->addStretch();

	QVBoxLayout * vbox = new QVBoxLayout(this);
	vbox->setMargin(2);
	vbox->addWidget(myListView);
	vbox->addLayout(hbox);

}

MyMainWindow::~MyMainWindow()
{
//	qDebug("~MyMainWindow()");
}

void MyMainWindow::onShowOffline()
{
	m_bShowOffline = !m_menuView->isItemChecked(viewShowOffline);
	m_menuView->setItemChecked(viewShowOffline, m_bShowOffline);

	for (user_item_t::Iterator it = user_item.begin(); it != user_item.end(); ++it)
	{
		MyListViewItem * item = it.data();

		if (!item->m_bAvailable)
			m_bShowOffline ? myListView->insertItem(item) : myListView->takeItem(item);			
	}
}

void MyMainWindow::onShowPictures()
{
	bool check = !m_menuView->isItemChecked(viewShowPictures);
	m_menuView->setItemChecked(viewShowPictures, check);

	myListView->m_bShowPicture = check;

	for (user_item_t::Iterator it = user_item.begin(); it != user_item.end(); ++it)
		it.data()->setup();

	myListView->update();
}

void MyMainWindow::onSortByName()
{
	bool check = !m_menuView->isItemChecked(viewSortByName);
	m_menuView->setItemChecked(viewSortByName, check);

	myListView->m_bSortByName = check;
	myListView->sort();
}

void MyMainWindow::addUser(QString user, bool bAvailable, bool bHavePhone, QString status)
{
	MyListViewItem * item;

	user_item_t::Iterator it = user_item.find(user);
	if (it != user_item.end())	
	{		
		item = it.data();

		if (!m_bShowOffline)
		{
			if (item->m_bAvailable)
			{
				if (!bAvailable)
					myListView->takeItem(item);
			}
			else
			{
				if (bAvailable)
					myListView->insertItem(item);
			}
		}
	}
	else
	{
		item = new MyListViewItem(myListView);
		item->setText(0, user);
		user_item[user] = item;
	
		!m_bShowOffline & !bAvailable ?
			myListView->takeItem(item) : myListView->insertItem(item);

	}

	item->m_bAvailable = bAvailable;
	item->m_bHavePhone = bHavePhone;
	item->m_sStatus = status;

	item->setup();
	myListView->sort();
}

void MyMainWindow::clickedAdd()
{
	QString user = QString("item %1").arg(rand() % 10);
	bool bAvailable = (rand() % 2 == 1);
	QString status = bAvailable && (rand() % 2 == 1) ? "3.6MiB / 101.0MiB used, 97.4MiB free" : "";
	bool bHavePhone = bAvailable ? (rand() % 2 == 1) : false;

	addUser(user, bAvailable, bHavePhone, status);
}
